package org.bonorsoft.modules.control.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.exception.WilliamBootException;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Hazard;
import org.bonorsoft.modules.control.entity.Label;
import org.bonorsoft.modules.control.mapper.LabelMapper;
import org.bonorsoft.modules.control.service.ILabelService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 标签管理
 * @Author: WilliamYe
 * @Date:   2021-03-19
 * @Version: V1.0
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements ILabelService {

    /**
     * 校验标签名称
     * @param label
     * @return
     */
    @Override
    public String checkLabelNameUnique(Label label) {
        long deptId = StringUtils.isNull(label.getLabelId()) ? -1L : label.getLabelId();
        Label info = checkDeptNameUnique(label.getLabelName(), label.getParentId());
        if (StringUtils.isNotNull(info) && info.getLabelId() != deptId)
        {
            return UserConstants.LABEL_NAME_NOT_UNIQUE;
        }
        return UserConstants.LABEL_NAME_UNIQUE;
    }

    /**
     * 加载标签列表树
     * @return
     */
    @Override
    public List<Ztree> selectLabelTree() {
        List<Label> list = this.list();
        return initZtree(list);
    }

    /**
     * 修改信息
     * @param label
     */
    @Override
    public boolean updateLabel(Label label) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        label.setUpdateBy(loginUser.getUsername()).setUpdateTime(new Date());
        return retBool(baseMapper.updateById(label));
    }

    @Override
    public Integer selectCountLabelByParentId(Long labelId) {
        return this.count(new QueryWrapper<Label>().eq("parent_id", labelId));
    }

    /**
     * 对象转菜单树
     *
     * @param labelList 源点列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<Label> labelList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Label label : labelList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(label.getLabelId());
            ztree.setpId(label.getParentId());
            ztree.setName(label.getLabelName());
            ztree.setTitle(label.getLabelName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    /**
     * 获取标签信息
     * @param labelName
     * @param parentId
     * @return
     */
    private Label checkDeptNameUnique(String labelName, Long parentId) {
        return this.getOne(new QueryWrapper<Label>().eq("label_name", labelName).eq("parent_id", parentId));
    }
}
